<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

class OrderController extends HomebaseController {
	public $Site;
	public $Coach;
	public $Lesson;
	public $Reserve;
	public $Member;
	//构造函数初始化各个表对象
	public function __construct(){
		parent::__construct();
		session(array('name'=>'session_id','expire'=>3600));
		$this->Site=M('site');
		$this->Coach=M('coach');
		$this->Lesson=M('lesson');
		$this->Reserve=M('reserve');
		$this->Member=M('member');
	}
	//预约(生成订单)
	public function order(){
		$left=$_GET['left'];
		$data['uid']=60008;//需要获取用户uid
		$data['lid']=$_GET['lid'];
		$lid=$_GET['lid'];
		$msg1=$this->Lesson->where("lid='$lid'")->select();
		//判断日期
		$d=str_replace('-','',$msg1[0]['sdate'])-date('Ymd',time());//大于0则可以预约，小于0不能预约，等于0则判断可否预约
		$h=date('Hi',$msg1[0]['stime'])-date('Hi',time());//小于2小时则在开课2小时以内
		if($d<0){
			echo "<script>alert('课程已结束,不能预约！');location.href='index.php?m=Choose&a=order_complete';</script>";
		}elseif($d==0&&$h<=0){
			echo"<script>alert('课程已开始，不能预约！');location.href='index.php?m=Choose&a=order_complete';</script>";
		}else{
		//判断是否有未评价课程
		//已评价，判断是否已预约
			$lid=$_GET['lid'];
			$uid=60008;
			$msgp=$this->Reserve->where("lid='$lid' AND uid='$uid'")->select();
			if($msgp){
				echo "<script>alert('您已经预约该课程！');location.href='index.php?m=Choose&a=order_complete';</script>";
				}else{
					//判断人数是否已满
					if($left==0){
						//已满，需要排队
						$data['status']=2;//排队状态
						$data['ordertime']=date("Y-m-d h:i:s");
						$msg=$this->Reserve->add($data);
						if($msg){
							echo "<script>alert('排队中！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}else{
							echo "<script>alert('排队失败！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}
					}else{
						//未满,预约成功
						$data['status']=1;
						$data['ordertime']=date("Y-m-d h:i:s");
						$msg=$this->Reserve->add($data);//添加订单
						$msg1=$this->Lesson->where("lid='$lid'")->select();
						$m['ordered']=$msg1[0]['ordered']+1;
						$msg2=$this->Lesson->where("lid='$lid'")->save($m);//ordered已预约人数+1
						if($msg&&$msg2){
							echo "<script>alert('预约成功！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}else{
							echo "<script>alert('预约失败！');location.href='index.php?m=Choose&a=order_complete';</script>";
						}
					}
				}
		}
	}
	//取消预约(取消预约)
	public function cancel(){
		$uid=60008;//需要获取用户uid
		$lid=$_GET['lid'];
		$msg1=$this->Lesson->where("lid='$lid'")->select();
		//判断日期
		$d=str_replace('-','',$msg1[0]['sdate'])-date('Ymd',time());//大于0则可以取消，小于0不能取消，等于0则判断可否取消
		$h=date('Hi',$msg1[0]['stime'])-date('Hi',time());//小于2小时则在开课2小时以内
		if($d<0){
			echo "<script>alert('课程已结束,不能取消！');location.href='index.php?m=Choose&a=order_complete';</script>";
		}elseif($d==0&&$h<=200){
			echo"<script>alert('开课前2小时不能取消！');location.href='index.php?m=Choose&a=order_complete';</script>";
		}else{
			$msg=$this->Reserve->where("uid='$uid' AND lid='$lid'")->delete();
			if($msg){
				//判断是否有人在排队中
				$wait=$this->Reserve->where("lid='$lid' AND status='2'")->order('ordertime')->select();
				if($wait){
					//若有人在排队，则更改第一个排队订单的status=1
					$where['reid']=$wait[0]['reid'];
					$save['status']='1';
					$waitdel=$this->Reserve->where($where)->sava($save);
				}
				$m['ordered']=$msg1[0]['ordered']-1;
				$msg2=$this->Lesson->where("lid='$lid'")->save($m);//ordered已订人数-1
				echo "<script>alert('取消成功！');location.href='index.php?m=Choose&a=order_complete';</script>";
			}else{
				echo "<script>alert('取消失败！');location.href='index.php?m=Choose&a=order_complete';</script>";
			}
		}
	}
}